import pygame
import math
import random
from config import *

class Enemy:
    def __init__(self, x, y, enemy_type="basic"):
        self.x = x
        self.y = y
        self.width = 30
        self.height = 30
        self.enemy_type = enemy_type
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        
        # 跳跃和重力相关属性
        self.velocity_y = 0
        self.on_ground = False
        
        # 攻击相关属性
        self.is_attacking = False
        self.attack_effect_time = 0
        self.attack_effect_pos = (0, 0)
        
        # 根据敌人类型设置属性
        if enemy_type == "basic":
            self.color = ENEMY_BASIC_COLOR
            self.speed = ENEMY_BASIC_SPEED
            self.health = ENEMY_BASIC_HEALTH
            self.max_health = ENEMY_BASIC_HEALTH
            self.attack_power = ENEMY_BASIC_ATTACK_POWER
            self.jump_power = ENEMY_BASIC_JUMP_POWER
            self.jump_probability = ENEMY_BASIC_JUMP_PROBABILITY
        elif enemy_type == "fast":
            self.color = ENEMY_FAST_COLOR
            self.speed = ENEMY_FAST_SPEED
            self.health = ENEMY_FAST_HEALTH
            self.max_health = ENEMY_FAST_HEALTH
            self.attack_power = ENEMY_FAST_ATTACK_POWER
            self.jump_power = ENEMY_FAST_JUMP_POWER
            self.jump_probability = ENEMY_FAST_JUMP_PROBABILITY
        elif enemy_type == "strong":
            self.color = ENEMY_STRONG_COLOR
            self.speed = ENEMY_STRONG_SPEED
            self.health = ENEMY_STRONG_HEALTH
            self.max_health = ENEMY_STRONG_HEALTH
            self.attack_power = ENEMY_STRONG_ATTACK_POWER
            self.jump_power = ENEMY_STRONG_JUMP_POWER
            self.jump_probability = ENEMY_STRONG_JUMP_PROBABILITY
            
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        self.facing_right = True  # 敌人面向方向
        self.last_jump_time = 0  # 上次跳跃时间，用于控制跳跃频率
        self.last_move_time = 0  # 上次移动时间，用于控制移动频率
    
    def move_towards_player(self, player_x, player_y, platforms):
        # 计算向玩家移动的方向（仅水平移动）
        dx = player_x - self.x
        distance = abs(dx)
        
        # 控制移动频率，减少不必要的计算
        current_time = pygame.time.get_ticks()
        if current_time - self.last_move_time < 50:  # 每50毫秒更新一次移动
            return
        
        self.last_move_time = current_time
        
        if distance > 0:
            # 设置面向方向
            if dx > 0:
                self.facing_right = True
            else:
                self.facing_right = False
                
            # 水平移动
            if dx > 0:
                self.x += self.speed
            else:
                self.x -= self.speed
            
            # 更新矩形位置
            self.rect.x = self.x
            
            # 应用重力
            self.velocity_y += PLAYER_GRAVITY
            self.y += self.velocity_y
            
            # 更新矩形位置
            self.rect.y = self.y
            
            # 检查平台碰撞
            self.on_ground = False
            for platform in platforms:
                if self.rect.colliderect(platform):
                    # 从上方落到平台上
                    if self.velocity_y > 0 and self.rect.bottom > platform.top and self.rect.top < platform.top:
                        self.rect.bottom = platform.top
                        self.y = self.rect.y
                        self.velocity_y = 0
                        self.on_ground = True
                    # 从下方撞到平台
                    elif self.velocity_y < 0 and self.rect.top < platform.bottom and self.rect.bottom > platform.bottom:
                        self.rect.top = platform.bottom
                        self.y = self.rect.y
                        self.velocity_y = 0
            
            # 地面边界检测
            if self.y > SCREEN_HEIGHT - self.height:
                self.y = SCREEN_HEIGHT - self.height
                self.rect.y = self.y
                self.velocity_y = 0
                self.on_ground = True
                
            if self.y < 0:
                self.y = 0
                self.rect.y = self.y
                self.velocity_y = 0
                
            # 随机跳跃，增加时间间隔控制
            if self.on_ground and current_time - self.last_jump_time > 1000:  # 至少1秒跳跃一次
                if random.randint(1, 100) <= self.jump_probability:  # 根据敌人类型使用不同的跳跃概率
                    self.velocity_y = -self.jump_power
                    self.on_ground = False
                    self.last_jump_time = current_time
    
    def take_damage(self, damage):
        # 受到伤害
        self.health -= damage
        if self.health < 0:
            self.health = 0
            return True  # 敌人被击败
        return False
    
    def attack_player(self, player):
        # 攻击玩家
        player.take_damage(self.attack_power)
        # 设置攻击特效
        self.is_attacking = True
        self.attack_effect_time = ATTACK_EFFECT_DURATION
        # 设置攻击特效位置（在玩家附近）
        self.attack_effect_pos = (player.x + player.width//2, player.y + player.height//2)
    
    def update(self):
        # 更新攻击特效
        if self.attack_effect_time > 0:
            self.attack_effect_time -= 1
            if self.attack_effect_time <= 0:
                self.is_attacking = False
    
    def draw(self, screen, camera):
        # 应用摄像机偏移
        draw_rect = camera.apply(self)
        
        # 绘制敌人（使用圆形表示）
        pygame.draw.rect(screen, self.color, draw_rect)
        pygame.draw.circle(screen, (100, 0, 0), (int(draw_rect.centerx), int(draw_rect.centery)), 
                          min(self.width, self.height)//2)
        
        # 绘制攻击特效
        if self.is_attacking and self.attack_effect_time > 0:
            effect_radius = 15 - self.attack_effect_time
            effect_pos = (self.attack_effect_pos[0] + camera.camera.x, 
                         self.attack_effect_pos[1] + camera.camera.y)
            pygame.draw.circle(screen, RED, effect_pos, effect_radius, 2)
        
        # 绘制血条
        health_bar_width = 40
        health_bar_height = 3
        health_ratio = self.health / self.max_health
        pygame.draw.rect(screen, RED, 
                        (draw_rect.x, draw_rect.y - 8, health_bar_width, health_bar_height))
        pygame.draw.rect(screen, GREEN, 
                        (draw_rect.x, draw_rect.y - 8, health_bar_width * health_ratio, health_bar_height))